package cc.linker.steplink.crm.merchant.service;


import cc.linker.steplink.crm.merchant.dao.CategoryDao;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    public List<Map<String,Object>> getPList(String orgId){
        List<Map<String, Object>> pList = categoryDao.getPList(orgId);
        for (Map<String, Object> l: pList) {
            List<Map<String, Object>> sList = categoryDao.getSList((Integer) l.get("categoryId"),orgId);
            if(null == sList || sList.size()==0){
                l.put("status",0);//当子类目为0个时，该类目状态禁用，不可启用
            }
            String subNames = "";
            for(Map<String, Object> s:sList){
                if(subNames.length()==0){
                    subNames = s.get("subName").toString();
                }else {
                    subNames = subNames+"、"+s.get("subName").toString();
                }
            }
            l.put("subList",sList);
            l.put("subNames",subNames);
        }
        return pList;
    }

    public List<Map<String,Object>> getSList (Integer pid,String orgId){
        List<Map<String, Object>> sList = categoryDao.getSList(pid,orgId);
        return sList;
    }

    public List<Map<String,Object>> getAllSList (Integer pid,String orgId){
        return categoryDao.getAllSList(pid,orgId);
    }

    public Map<String, Object> getDetail(Integer categoryId,String orgId){
        return categoryDao.getDetail(categoryId,orgId);
    }

    public boolean changeStatus(Integer categoryId,String orgId){
        Map<String, Object> detail = categoryDao.getDetail(categoryId,orgId);
        if(null == detail || !"1".equals(detail.get("grade").toString())){
            throw GlobalException.build("参数错误");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("categoryId",categoryId);
        data.put("orgId",orgId);
        if(0==(Integer)detail.get("status")){
            Integer sListCount = categoryDao.getSListCount(categoryId,orgId);
            if(sListCount == 0){
                throw GlobalException.build("当前类目没有子类目，启用失败");
            }
            data.put("status",1);
        }else{
            data.put("status",0);
        }
        return categoryDao.changeStatus(data);
    }

    public boolean addsCategory(Map<String,Object> data){
        return categoryDao.addsCategory(data);
    }

    public boolean renameSub(Map<String,Object> data){
        return categoryDao.renameSub(data);
    }

    public boolean removesCategory(Map<String,Object> data){
        if(categoryDao.getGoodsCate(Integer.parseInt(data.get("subId").toString()))>0){
            throw GlobalException.build("该类目下存在商品，无法删除");
        }
        return categoryDao.removesCategory(data);
    }

    public boolean cheackRename(String cateName,String orgId){
        return categoryDao.cheackRename(cateName,orgId)>0;
    }

    public List<Map<String,Object>> getAttributeList(Integer categoryId,String orgId){
        List<Map<String, Object>> attributeList = categoryDao.getAttributeList(categoryId, 1, 0,orgId);
        for(Map<String, Object> at:attributeList){
            List attributeId = getSonAttributeList(categoryId, (Integer) at.get("attributeId"),orgId);
            at.put("sList",attributeId);
        }
        return attributeList;
    }
    public List getSonAttributeList(Integer categoryId,Integer pid,String orgId){
        List<Map<String, Object>> attributeList = categoryDao.getAttributeList(categoryId, null, pid,orgId);
        for(Map<String, Object> at:attributeList){
            if((Integer)at.get("grade") == 1 || (Integer)at.get("grade") == 2){
                List attributeId = getSonAttributeList(categoryId, (Integer) at.get("attributeId"),orgId);
                at.put("sList",attributeId);
            }
        }
        return attributeList;
    }

    public Map getGoodsCateAttr(Long goodsId,String orgId){
        Map goodsCateAttr = categoryDao.getGoodsCateAttr(goodsId);
        Map<String, Object> goodsCateAttrs = new HashMap<>();
        if(null != goodsCateAttr){
            Integer scateId = (Integer) goodsCateAttr.get("categoryId");
            String attrIds = (String) goodsCateAttr.get("attributeId");
            Integer pcateId = categoryDao.getpcateId(scateId,orgId);
            Map<String,Integer> cate = new HashMap<>();
            cate.put("pcateId",pcateId);
            cate.put("scateId",scateId);
            goodsCateAttrs.put("cate",cate);

            String[] attrIdSplit = attrIds.split(",");
            Set<String> attrSet = new HashSet<>(Arrays.asList(attrIdSplit));
            List<Map<String, Object>> attributeList = getAttributeList(pcateId,orgId);
            for(Map<String, Object> first:attributeList){//第一层
                first.put("ifSelected",0);
                List<Map<String, Object>> secList = (List)first.get("sList");
                if(null!=secList && secList.size()>0){
                    for(Map<String, Object>sec:secList){//第二层
                        sec.put("ifSelected",0);
                        if(attrSet.contains(sec.get("attributeId").toString())){
                            sec.put("ifSelected",1);
                            first.put("ifSelected",1);
                        }
                        List<Map<String, Object>> thrList = (List)sec.get("sList");
                        if(null!=thrList && thrList.size()>0){
                            for(Map<String, Object> thr:thrList){//第三层
                                thr.put("ifSelected",0);
                                if(attrSet.contains(thr.get("attributeId").toString())){
                                    thr.put("ifSelected",1);
                                    sec.put("ifSelected",1);
                                    first.put("ifSelected",1);
                                }
                            }
                        }
                    }
                }
            }
            goodsCateAttrs.put("attr",attributeList);
        }
        return goodsCateAttrs;
    }
}
